package com.zito.health.service.impl;
import com.zito.health.common.service.impl.BaseServiceImpl;
import com.zito.health.dto.LaboratoryDto;
import com.zito.health.entity.Laboratory;
import com.zito.health.entity.LaboratoryExamine;
import com.zito.health.mapper.LaboratoryAppMapper;
import com.zito.health.mapper.LaboratoryExamineMapper;
import com.zito.health.service.LaboratoryAppService;
import com.zito.health.vo.LaboratoryAppVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 实验室app实现类
 * @auth sun
 */
@Service
public class LaboratoryAppServiceImpl extends BaseServiceImpl<LaboratoryAppVo> implements LaboratoryAppService {
    @Autowired
    private LaboratoryAppMapper laboratoryAppMapper;
    @Autowired
    private LaboratoryExamineMapper laboratoryExamineMapper;
    /**
     * 查询详情
     * @param id
     * @return
     * @auth sun
     */
    @Override
    public LaboratoryAppVo queryByid(Integer id) {
        return laboratoryAppMapper.queryByid(id);
    }

    /**
     * 查询列表
     * @auth sun
     */
    public List<LaboratoryAppVo> queryList(Laboratory laboratory){
        return laboratoryAppMapper.queryList(laboratory);
    }

    /**
     * 区/县审核
     * @auth sun
     */
    @Transactional
    @Override
    public void countyAudit(LaboratoryDto laboratoryDto) {
        laboratoryAppMapper.audit(laboratoryDto);
        LaboratoryExamine laboratoryExamine = new LaboratoryExamine();
        laboratoryExamine.setLaboratoryId(laboratoryDto.getId());
        // 查询是否审核过了 审核过了修改县审核意见 没有审核过的就创建一个
        List laboratoryExamineList = laboratoryExamineMapper.select(laboratoryExamine);
        if (laboratoryExamineList.size()>0) {
            laboratoryExamine.setOpinionCounty(laboratoryDto.getOpinionCounty());
            laboratoryExamine.setUserIdCounty(laboratoryDto.getUserIdCounty());
//            // 再次审核清空市审核意见
//            laboratoryExamine.setOpinionCity(" ");
//            laboratoryExamine.setUserIdCity(0);
            laboratoryExamineMapper.updateByPrimaryKeySelective(laboratoryExamine);
        }else{
            laboratoryExamineMapper.countyAudit(laboratoryDto);
        }

    }

    /**
     * 市审核
     * @auth sun
     */
    @Transactional
    @Override
    public void cityAudit(LaboratoryDto laboratoryDto) {
        laboratoryAppMapper.audit(laboratoryDto);
        laboratoryExamineMapper.cityAudit(laboratoryDto);
    }
}
